package com.quickdev.intelligenthome.net
{
	import com.quickdev.intelligenthome.config.Config;
	
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.events.Request;
	
	import org.osmf.events.TimeEvent;
	
	public class CallBackLoader extends URLLoader
	{
		public var callBack :Function = null;
		
		private var timer :Timer = null;
		
		private var count :uint = 0;
		
		private var _req :URLRequest = null;
		
		public function CallBackLoader(request:URLRequest=null)
		{
			super(request);
			
			_req = request;
			
			addEventListener(Event.OPEN, onOpen);
			//addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onResponseStatus);
			addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatus);
			addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		override public function load(request:URLRequest):void {
			super.load(request);
			timer = new Timer( Config.delay );
			timer.addEventListener(TimerEvent.TIMER, onTimer);
		}
		
		private function onTimer( event :TimeEvent ) :void {
			if( bytesLoaded == bytesTotal ) {
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onTimer);
				timer = null;
			}
			else {
				close();
				if( count < Config.repeatCount ) {
					load(_req);
					++count;
				}
				else {
					trace("网络连接断开");
				}
			}
		}
		
		private function onOpen( event :Event ) :void {
			
		}
		
		private function onResponseStatus( event :HTTPStatusEvent ) :void {
			
		}
		
		private function onHttpStatus( event :HTTPStatusEvent ) :void {
			
		}
		
		private function onIoError( event :IOErrorEvent ) :void {
			
		}
		
		private function onSecurityError( event :SecurityErrorEvent ) :void {
			
		}
	}
}